$(document).ready(function() {
});

function Search(){

	var peNo = $("#inputPeNo").val();
	var month =$("#inputMonth").val();
	var year =$("#inputYear").val();
	
	if (month == "" || year == "") {
		document.getElementById("modal_content").innerHTML = "กรุณาเลือกดือนและปีที่จะค้นหา";
		$('#warningModal').modal();
	}else{

		$.ajax({
			url : 'ACM1102Srvl', 
			type : "POST",
			data : {
				peNo: peNo,
				month:month,
				year: year
			},
			dataType : "json",
			success: function(data){
				var message = "";
				message +="<table class='table-list table-bordered table-hover'>";
				message +=" <thead>";
				message +=" <tr>";
				message +=" <th>ลำดับ</th>"; 
				message +=" <th>รหัสตัวแทน</th>";  
				message +=" <th>ชื่อสกุล</th>";  
				message +=" <th>NET MOV</th>";  
				message +=" <th>จัดการ</th>";  
				message +=" </tr>";
				message +=" </thead>";   
				message +=" <tbody>";  
				message +=" <tr>";
				for (var i = 0; i < data.PeNo.length; i++) {
					var seq= i+1;
				message +=" <td>"+seq+"</td>";
				message +=" <td>"+data.PeNo[i]+"</td>";
				message +=" <td>"+data.name[i]+"</td>";
				message +=" <td>"+data.net[i]+"</td>";
				message +=" <td><button class='btn btn-primary mr-5' onclick='next("+data.PeNo[i]+");'><i class='fa fa-external-link' title='ดูรายละเอียด' id='editButton' ></i></button><button class='btn btn-primary' onclick='details("+data.PeNo[i]+","+year+","+0+month+");' ><i class='fa fa-search' title='รายละเอียดPopUp' id='editButton' ></i></button></td>";
				message +=" </tr>";
				}
				message +=" </tbody>";   
				message +=" </table>";   
				$("#table-search").html(message);
			}
			
		});
	}
	
}

function next(PeNo){
	//alert(PeNo);
	$("#PeNo").val(PeNo);
	//document.getElementById("details").submit();

}

function details(PeNo,year,month){
	//alert("details"+PeNo+"  "+0+month+"  "+year);
	var months = "0"+month;
	//alert(months);
	$.ajax({
		url : 'ACM1102Srvl', 
		type : "GET",
		data : {
			peNo: PeNo,
			month:months,
			year: year
		},
		dataType : "json",
		success: function(data){
			var message = "";
			message +="<table class='table-list table-bordered table-hover'>";
			message +=" <thead>";
			message +=" <tr>";
			message +=" <th>ลำดับ</th>"; 
			message +=" <th>รหัสตัวแทน</th>";  
			message +=" <th>ชื่อสกุล</th>";  
			message +=" <th>Duty Code</th>";  
			message +=" <th>PC</th>";  
			message +=" </tr>";
			message +=" </thead>";   
			message +=" <tbody>";  
			
			for (var i = 0; i < parseInt(data.PeNo.length) ; i++) {
				var seq= i+1;
			message +=" <tr>";
			message +=" <td>"+seq+"</td>";
			message +=" <td>"+data.PeNo[i]+"</td>";
			message +=" <td>"+data.name[i]+"</td>";
			message +=" <td>"+data.duty[i]+"</td>";
			message +=" <td>"+data.pc[i]+"</td>";
			message +=" </tr>";
			}
			message +=" <tr>";
			message +=" <td colspan='4'>Total PC</td>";
			message +=" <td >"+data.total+"</td>";
			message +=" </tr>";
			message +=" </tbody>";   
			message +=" </table>";   
			
			document.getElementById("table-details").innerHTML= message;
			
			$("#detailsModal").modal('show');
		}
		
	});
	
	
	
}








